<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Home\TbkItemGetRequest;
use App\Http\Controllers\Home\TopClient;
use App\Http\Controllers\Home\TbkItemInfoGetRequest;
use App\Models\Icon;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use App\Models\WheelPicture;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    /**
     * 后台布局
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function layout()
    {

        /*$c = new TopClient();
        $c->appkey = '25828508';
        $c->secretKey = 'c314651711d8e00a4c0c631cb9a1dc3c';
        $req = new TbkItemGetRequest();
        $req->setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick");
        $req->setQ("女装");
        $req->setPlatform("1");
        $resp = $c->execute($req);*/

        /*$c = new TopClient();
        $c->appkey = '25828508';
        $c->secretKey = 'c314651711d8e00a4c0c631cb9a1dc3c';
        $req = new TbkItemInfoGetRequest();
        $req->setNumIids("586013125500");
        $resp = $c->execute($req);

        echo "<pre>";
        var_dump(json_decode(json_encode($resp),true));exit;*/

        $expTime=null;
        try{
            $url='http://tkapi.apptimes.cn/user/exp-time?appkey='.env('TKAPI_APP_KEY').'&appsecret='.env('TKAPI_APP_SECRET');
            $res=curl_request($url);
            if($res) {
                if(isset(json_decode($res,true)['data']['exp_time']))
                    $expTime=json_decode($res,true)['data']['exp_time'];
            }
            if(!empty($expTime)){
                $timer=timer($expTime);
                if($timer['day']>0){
                    $expTime=$timer['day'].'天';
                }else{
                    $expTime=$timer['str'];
                    if($timer['second']<0)
                        $expTime=null;
                }
            }
        }catch (\Exception $e){}

        return view('admin.layout',compact('expTime'));
    }

    /**
     * 后台首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('admin.index.index');
    }
    public function index1()
    {
        return view('admin.index.index1');
    }
    public function index2()
    {
        return view('admin.index.index2');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 数据表格接口
     */
    public function data(Request $request)
    {
        $model = $request->get('model');
        switch (strtolower($model)) {
            case 'user':
                $query = new User();
                break;
            case 'role':
                $query = new Role();
                break;
            case 'permission':
                $query = new Permission();
                $query = $query->where('parent_id', $request->get('parent_id', 0))->with('icon');
                break;
            case 'wheelpicture':
                $query = new WheelPicture();
                break;
            default:
                $query = new User();
                break;
        }
        $res = $query->paginate($request->get('limit', 30))->toArray();
        $data = [
            'code' => 0,
            'msg' => '正在请求中...',
            'count' => $res['total'],
            'data' => $res['data']
        ];
        return response()->json($data);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 所有icon图标
     */
    public function icons()
    {
        $icons = Icon::orderBy('sort', 'desc')->get();
        return response()->json(['code' => 0, 'msg' => '请求成功', 'data' => $icons]);
    }
    public function clearCache(){
        $cache=Cache();
        $res=$cache->clear();
        $flag=false;
        if($res)
            $flag=true;
        return view('admin.index.clear_cache',compact('flag'));
    }
}
